package com.decent.ejfadmin.user.dao;

import com.decent.ejfadmin.common.bean.*;
import com.decent.ejfadmin.common.enums.SubmitCardApplyEnum;
import com.decent.ejfadmin.user.dao.provider.UserProvider;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.math.BigDecimal;
import java.util.List;

/**
 * 会员信息
 *
 * @author weicx
 * @date 2018/07/25
 */
@Mapper
@Repository
public interface UserDao {
    /**
     * 模糊查询会员id
     *
     * @param accountName 会员编号或名称
     * @return
     */
    @Select("SELECT\n" +
            "\tu.user_id\n" +
            "FROM\n" +
            "\tyuhuang_sup_user u,\n" +
            "\tyuhuang_sup_info i\n" +
            "WHERE\n" +
            "\tu.user_id = i.user_id\n" +
            "AND (\n" +
            "\tinstr(\n" +
            "\t\tu.account,\n" +
            "\t\t#{accountName})>0 or instr(i.name,#{accountName})>0)")
    List<Integer> queryUserIdByBlurName(@Param("accountName") String accountName);

    /**
     * 查询会员
     *
     * @param userId 会员id
     * @return
     */
    @Select("SELECT u.user_id userId,u.account account,u.status,u.pm_id pmId,u.login_Pwd loginPwd,u.equity_credit equityCredit," +
            "u.pay_Pwd  payPwd,u.user_Key userKey,u.recommend_id recommendId,u.api_state apiState,u.supply_model supplyModel,u.ip," +
            "info.name 'supInfo.name',info.email 'supInfo.email',info.open_id 'supInfo.openId',pay_Pwd payPwd FROM yuhuang_sup_user u,yuhuang_sup_info info WHERE u.user_id=info.user_id\n" +
            "AND u.user_id=#{userId} ")
    SupUser queryUserById(@Param("userId") Long userId);

    /**
     * 根据条件查询会员数量
     *
     * @param supUser   会员实体
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param include   0包含，1剔除
     * @param auditType 供卡审核状态
     * @return 会员数量
     */
    @SelectProvider(type = UserProvider.class, method = "querySupUserCount")
    int querySupUserCount(@Param("supUser") SupUser supUser, @Param("startTime") String startTime,
                          @Param("endTime") String endTime, @Param("include") Integer include, @Param("auditType") String auditType);

    /**
     * 根据条件查询会员
     *
     * @param supUser   会员实体
     * @param page      分页组件
     * @param startTime 查询开始时间
     * @param endTime   查询结束时间
     * @param include   0包含，1剔除
     * @param auditType 供卡审核状态
     * @return
     */
    @SelectProvider(type = UserProvider.class, method = "querySupUser")
    List<SupUser> querySupUser(@Param("supUser") SupUser supUser, @Param("page") Page page, @Param("startTime") String startTime,
                               @Param("endTime") String endTime, @Param("include") Integer include, @Param("auditType") String auditType);

    /**
     * 根据账号查询用户
     *
     * @param account 用户账号
     * @return 返回用户信息
     */
    @Select("SELECT  " +
            "  user_id userId, " +
            "  ACCOUNT, " +
            "  STATUS, " +
            "  pm_id pmId, " +
            "  login_Pwd loginPwd, " +
            "  pay_Pwd  payPwd, " +
            "  user_Key userKey,   " +
            "  recommend_id recommendId,  " +
            "  api_state apiState,  " +
            "  supply_model supplyModel,  " +
            "  ip   " +
            "FROM " +
            "  YUHUANG_SUP_USER  WHERE ACCOUNT = #{account}")
    SupUser querySupUsersByAccount(@Param("account") String account);

    /**
     * 根据IP查询会员编号,名称
     *
     * @param ip
     * @return
     */
    @Select("SELECT u.account,i.name 'supInfo.name' from yuhuang_sup_user u,yuhuang_sup_info i where u.user_id=i.user_id and i.ip=#{ip}")
    List<SupUser> downloadUserInfoList(@Param("ip") String ip);

    /**
     * 修改会员
     *
     * @param supUser 收卡会员信息
     * @return 修改行数
     */
    @UpdateProvider(type = UserProvider.class, method = "updateUser")
    int modifySupUser(@Param("supUser") SupUser supUser);

    /**
     * 修改会员权限
     *
     * @param supPrivilege 权限
     * @return 创建条数
     */
    @UpdateProvider(type = UserProvider.class, method = "modifySupPrivilege")
    int modifySupPrivilege(@Param("supPrivilege") SupPrivilege supPrivilege);

    /**
     * 修改会员info信息
     *
     * @param supInfo 会员信息
     * @return
     */
    @UpdateProvider(type = UserProvider.class, method = "updateUserInfo")
    int modifySupUserInfo(@Param("supInfo") SupInfo supInfo);

    /**
     * 查询会员
     *
     * @return
     */
    @Select("select u.user_id userId,u.account account,i.name name  from yuhuang_sup_user u,yuhuang_sup_info i where u.user_id=i.user_id order by u.user_id ")
    List<SupUser> queryAllUserNotParam();

    /**
     * 获取会员权限
     *
     * @param userId 用户id
     * @return 返回全下列表
     */
    @Select(" SELECT " +
            " u.status , " +
            " u.api_state apiState, " +
            " p.user_id userId, " +
            " p.business_overview businessOverview, " +
            " p.pay_pwd payPwdState, " +
            " p.recommend, " +
            " p.statistic_report statisticReport, " +
            " p.virtual_operate virtualOperate, " +
            " p.withdraw_state withdrawState, " +
            " p.post_paid postPaid, " +
            " p.supply supplyState," +
            " p.withdraw_state withdrawState, " +
            " p.alipay_withdraw alipayWithdraw, " +
            " p.pop_up popUp, " +
            " p.qb_exchange qbExchange " +
            "FROM " +
            " yuhuang_sup_user u,yuhuang_sup_privilege p " +
            "WHERE u.user_id=p.user_id and" +
            " p.user_id = #{userId} ")
    SupPrivilege querySupPrivilege(@Param("userId") Long userId);

    /**
     * 新增用户信息
     *
     * @param supUser 用户信息
     * @return 返回条数
     */
    @Insert("INSERT INTO `yuhuang_sup_user` ( " +
            " `account`, " +
            " `user_Key`, " +
            " `ip`, " +
            " `type`, " +
            " `status`," +
            "api_state," +
            " pm_id, " +
            " notify_url, " +
            " equity_credit, " +
            " register_remark, daily_supply_limit, entity_limit " +
            ") " +
            "VALUES " +
            " ( " +
            "  #{supUser.account}, " +
            "  #{supUser.userKey}, " +
            "  #{supUser.ip}, " +
            "  #{supUser.type}, " +
            "  #{supUser.status}, " +
            "  #{supUser.apiState}, " +
            "  #{supUser.pmId}, " +
            "  #{supUser.notifyUrl}, " +
            "  #{supUser.equityCredit}, " +
            "  #{supUser.registeRemark}, #{supUser.dailySupplyLimit}, #{supUser.entityLimit} " +
            " )")
    int insertSupUser(@Param("supUser") SupUser supUser);

    /**
     * 新增用户信息详情
     *
     * @param supInfo 信息bean
     * @return 返回结果
     */
    @InsertProvider(type = UserProvider.class, method = "createSupUserInfo")
    int createSupUserInfo(@Param("supInfo") SupInfo supInfo);

    /**
     * 创建会员权限
     *
     * @param userId 用户id
     * @return 创建条数
     */
    @Insert(" INSERT INTO yuhuang_sup_privilege (user_id) values ( #{userId} )  ")
    int createSupPrivilege(@Param("userId") Long userId);

    /**
     * 修改会员权益信用分
     *
     * @param userId       会员编号
     * @param equityCredit 权益信用分
     * @return
     */
    @Update("UPDATE yuhuang_sup_user SET equity_credit=#{equityCredit} WHERE user_id=#{userId}")
    int updateEquityCredit(@Param("userId") Long userId, @Param("equityCredit") BigDecimal equityCredit);

    /**
     * 新增权益信用分修改记录
     *
     * @param equityCreditModify 权益信用分
     * @return
     */
    @Insert("insert into yuhuang_credit_modify_record(userId,original_credit,new_credit,admin,remark,modify_time) " +
            "values(#{equityCreditModify.userId},#{equityCreditModify.originalCredit},#{equityCreditModify.newCredit}," +
            "#{equityCreditModify.admin},#{equityCreditModify.remark},now())")
    int insertEquityCreditModify(@Param("equityCreditModify") EquityCreditModify equityCreditModify);

    /**
     * 根据条件查询会员数量
     *
     * @param equityCreditModify 会员实体
     * @param startTime          查询开始时间
     * @param endTime            查询结束时间
     * @return
     */
    @SelectProvider(type = UserProvider.class, method = "queryEquityCreditModifyCount")
    int queryEquityCreditModifyCount(@Param("equityCreditModify") EquityCreditModify equityCreditModify, @Param("startTime") String startTime, @Param("endTime") String endTime);

    /**
     * 根据条件查询会员
     *
     * @param equityCreditModify 会员实体
     * @param page               分页组件
     * @param startTime          查询开始时间
     * @param endTime            查询结束时间
     * @return
     */
    @SelectProvider(type = UserProvider.class, method = "queryEquityCreditModify")
    List<EquityCreditModify> queryEquityCreditModify(@Param("equityCreditModify") EquityCreditModify equityCreditModify, @Param("page") Page page, @Param("startTime") String startTime,
                                                     @Param("endTime") String endTime);

    /**
     * 修改sup用户提卡申请
     *
     * @param userId 用户id
     * @param apply  申请状态
     */
    @Update("update yuhuang_sup_user set submit_card_apply = #{apply} where user_id = #{userId}")
    void modifySubmitCardApply(@Param("userId") Long userId, @Param("apply") SubmitCardApplyEnum apply);

    /**
     * 分页查询会员操作记录计数
     *
     * @param userId 会员id
     * @return 总数
     */
    @Select("select count(1) from yuhuang_sup_user_record where user_id = #{userId}")
    int queryUserRecordCount(@Param("userId") Long userId);

    /**
     * 分页查询会员操作记录
     *
     * @param page   分页组件
     * @param userId 会员id
     * @return list
     */
    @SelectProvider(type = UserProvider.class, method = "queryUserRecordPage")
    List<SupUserRecord> queryUserRecordPage(@Param("page") Page<SupUserRecord> page, @Param("userId") Long userId);
}
